module.exports = () => {
    const assert = require('http-assert')
    const AdminUser = require('../models/AdminUser')
    const jwt = require('jsonwebtoken')

    return async (req, res, next) => {
        // 判断token是否存在
        const token = String(req.headers.token || '').split(' ').pop()
        assert(token, 401, '请先登录');
        // 验证token
        const {id} = jwt.verify(token, require('../config/jwt').secret)
        assert(id, 401, '请先登录');
        req.user = await AdminUser.findById(id)
        await next()
    }
}